<?php
namespace Admin\Controller;
use Think\Controller;
class CarTypeController extends PublicController {
    public function index(){
    	$this->display();
    } 
 
     //商品展示
    public function get_all_car_list(){
  	   $limit = I('get.limit');
       $page = I('get.page');
       $like = I('get.like');
       if(!empty($like)){
          $where['pinyin|name']=array("like","%".$like."%");
       }

       $ret['total'] = M('car_type')->count();
       $ret['data'] = M('car_type')->where($where)->page($page,$limit)->select();
       foreach ($ret['data'] as $key => $value) {
          $ret['data'][$key]['logosrc'] = '<img style="width:50px;" src="'.$ret['data'][$key]['logo'].'" >';
        }
       $ret['code'] = 200;
       ret($ret);
    }

    //单图上传
    public function car_ico(){
      $name = date("Ymd_His_",time()).rand(100000,999999);
      copy($_FILES['file']['tmp_name'],'Public/upload/cartype/'.$name.'.jpg');
      $ret['isSuccess'] = 1;
      $ret['src'] = '/Public/upload/cartype/'.$name.'.jpg';
      ret($ret);
    }

    //添加商品保存
    public function add_car(){
      $data['name']=I('post.name');
      $data['pinyin']=I('post.pinyin');
      $data['logo']=I('post.logo');
      $res=M('car_type')->add($data);
      if($res){
         $ret['isSuccess'] = 1;
         $ret['msg'] = "添加成功";
         ret($ret);
      }else{
        $ret['isSuccess'] = 0;
        $ret['msg'] = "添加失败";
        ret($ret);
      }
  
    }

    //编辑商品保存
    public function save_car(){
        $data['id']=I('post.id');
        $data['name']=I('post.name');
        $data['pinyin']=I('post.pinyin');
        if(!empty(I('post.file'))){
          $data['logo']=I('post.file');
        }
        $res=M('car_type')->where(array('id'=>$data['id']))->save($data);
        if($res!==fasle){
          $ret['isSuccess'] = 1;
          $ret['msg'] = "修改成功";
          ret($ret);
          }else{
            $ret['isSuccess'] = 0;
            $ret['msg'] = "修改失败";
            ret($ret);
          }
    }

     //删除商品
    public function del_car(){
      $data['id']=I('post.id');
      $res=M('car_type')->where(array('id'=>$data['id']))->delete();
      if($res){
            $ret['isSuccess'] = 1;
               ret($ret);
            }else{
              $ret['isSuccess'] = 0;
               ret($ret);
            }
    }

  
}